package mybatis;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import mybatis.entity.GenEntity;
import java.util.HashMap;
import java.util.Map;


/**
 * @author dogger
 * 2021-10-15 18:05
 */
public class MybatisPlusGen {

    public static void gen(GenEntity entity) {
        Map<String, String> map = new HashMap<>(4);
        FastAutoGenerator.create(entity.getUrl(), entity.getUserName(), entity.getPassword())
                .globalConfig(builder -> builder
                        .author("dogger")
                       // .fileOverride()
                        .disableOpenDir()
                        .outputDir(entity.getOutputDir())
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                        //设置父包名
                        .parent(entity.getParentPackage())
                        .controller("controller")
                        .entity("domain.entity")
                        .other("domain")
                )
                .injectionConfig(builder -> builder
                        .beforeOutputFile((tableInfo, objectMap) -> {
                            String entityName = tableInfo.getEntityName();
                            map.put("/bo/"+entityName + "BO.java", entity.getTemplatePath() + "/entityBO.java.btl");
                            map.put("/dto/"+entityName + "DTO.java", entity.getTemplatePath() + "/entityDTO.java.btl");
                            map.put("/vo/"+entityName + "VO.java", entity.getTemplatePath() + "/entityVO.java.btl");
                            map.put("/vo/"+entityName + "Detail.java", entity.getTemplatePath() + "/entityDetail.java.btl");
                            //System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
                        }) //输出文件之前消费者
                        //.customMap(Collections.singletonMap("my_field", "你好！这是我自己注入的属性哦")) //自定义属性,map<自定义代码,生成后显示的参数>
                        .customFile(map) //自定义配置模板文件
                        .build()//加入构建队列
                )
                .strategyConfig(builder -> builder
                        .addTablePrefix("t_")//去掉前缀
                        .addInclude(entity.getTables())//生成表名
                        .enableCapitalMode()
                        .entityBuilder()
                        .enableLombok()
                        .enableChainModel()
                        .enableColumnConstant()
                        .logicDeleteColumnName("validity")
                        .logicDeletePropertyName("validity")

                )
                .templateConfig(builder -> builder
                        .entity(entity.getTemplatePath() + "/entity.java")
                        .controller(entity.getTemplatePath() + "/controller.java")
                        .service(entity.getTemplatePath() + "/service.java")
                        .serviceImpl(entity.getTemplatePath() + "/serviceImpl.java")
                        .mapper(entity.getTemplatePath() + "/mapper.java")
                        .xml(entity.getTemplatePath() + "/mapper.xml")
                )
                .templateEngine(new BeetlTemplateEngine())
                .execute();
    }
}
